<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce">
							<el-col v-if="$check_field('get','club_name') || $check_field('add','club_name') || $check_field('set','club_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="社团名称" prop="club_name">
												<el-input id="club_name" v-model="form['club_name']" placeholder="请输入社团名称"
							  v-if="(form['course_selection_record_id'] && $check_field('set','club_name')) || (!form['course_selection_record_id'] && $check_field('add','club_name'))" :disabled="disabledObj['club_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','club_name')">{{form['club_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','course_name') || $check_field('add','course_name') || $check_field('set','course_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="课程名称" prop="course_name">
												<el-input id="course_name" v-model="form['course_name']" placeholder="请输入课程名称"
							  v-if="(form['course_selection_record_id'] && $check_field('set','course_name')) || (!form['course_selection_record_id'] && $check_field('add','course_name'))" :disabled="disabledObj['course_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','course_name')">{{form['course_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','maximum_number_of_people') || $check_field('add','maximum_number_of_people') || $check_field('set','maximum_number_of_people')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="人数上限" prop="maximum_number_of_people">
								<el-input-number id="maximum_number_of_people" v-model.number="form['maximum_number_of_people']"
						v-if="(form['course_selection_record_id'] && $check_field('set','maximum_number_of_people')) || (!form['course_selection_record_id'] && $check_field('add','maximum_number_of_people'))" :disabled="disabledObj['maximum_number_of_people_isDisabled']"></el-input-number>
					<div v-else-if="$check_field('get','maximum_number_of_people')">{{form['maximum_number_of_people']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','current_number_of_people') || $check_field('add','current_number_of_people') || $check_field('set','current_number_of_people')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="当前人数" prop="current_number_of_people">
								<el-input-number id="current_number_of_people" v-model.number="form['current_number_of_people']"
						v-if="(form['course_selection_record_id'] && $check_field('set','current_number_of_people')) || (!form['course_selection_record_id'] && $check_field('add','current_number_of_people'))" :disabled="disabledObj['current_number_of_people_isDisabled']"></el-input-number>
					<div v-else-if="$check_field('get','current_number_of_people')">{{form['current_number_of_people']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','teacher_users') || $check_field('add','teacher_users') || $check_field('set','teacher_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="教师用户" prop="teacher_users">
													<el-select v-if="(form['course_selection_record_id'] && $check_field('set','teacher_users')) || (!form['course_selection_record_id'] && $check_field('add','teacher_users'))" id="teacher_users" v-model="form['teacher_users']" :disabled="disabledObj['teacher_users_isDisabled']">
							<el-option v-for="o in list_user_teacher_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','teacher_users')" id="teacher_users" v-model="form['teacher_users']" :disabled="true">
							<el-option v-for="o in list_user_teacher_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','student_users') || $check_field('add','student_users') || $check_field('set','student_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生用户" prop="student_users">
																		<div v-if="user_group !== '管理员'">
							{{ get_user_session_student_users(form['student_users']) }}
							<el-select v-if="(form['course_selection_record_id'] && $check_field('set','student_users')) || (!form['course_selection_record_id'] && $check_field('add','student_users'))" id="student_users" v-model="form['student_users']" :disabled="disabledObj['student_users_isDisabled']">
								<el-option v-for="o in list_user_student_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','student_users')" id="student_users" v-model="form['student_users']" :disabled="true">
								<el-option v-for="o in list_user_student_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="student_users" v-model="form['student_users']" :disabled="disabledObj['student_users_isDisabled']">
							<el-option v-for="o in list_user_student_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','student_name') || $check_field('add','student_name') || $check_field('set','student_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生姓名" prop="student_name">
												<el-input id="student_name" v-model="form['student_name']" placeholder="请输入学生姓名"
							  v-if="(form['course_selection_record_id'] && $check_field('set','student_name')) || (!form['course_selection_record_id'] && $check_field('add','student_name'))" :disabled="disabledObj['student_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_name')">{{form['student_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','student_phone_number') || $check_field('add','student_phone_number') || $check_field('set','student_phone_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生电话" prop="student_phone_number">
												<el-input id="student_phone_number" v-model="form['student_phone_number']" placeholder="请输入学生电话"
							  v-if="(form['course_selection_record_id'] && $check_field('set','student_phone_number')) || (!form['course_selection_record_id'] && $check_field('add','student_phone_number'))" :disabled="disabledObj['student_phone_number_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_phone_number')">{{form['student_phone_number']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','student_id') || $check_field('add','student_id') || $check_field('set','student_id')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生学号" prop="student_id">
												<el-input id="student_id" v-model="form['student_id']" placeholder="请输入学生学号"
							  v-if="(form['course_selection_record_id'] && $check_field('set','student_id')) || (!form['course_selection_record_id'] && $check_field('add','student_id'))" :disabled="disabledObj['student_id_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_id')">{{form['student_id']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','number_of_electors') || $check_field('add','number_of_electors') || $check_field('set','number_of_electors')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="选课人数" prop="number_of_electors">
								<el-select id="number_of_electors" v-model="form['number_of_electors']"
						v-if="(form['course_selection_record_id'] && $check_field('set','number_of_electors')) || (!form['course_selection_record_id'] && $check_field('add','number_of_electors'))">
						<el-option v-for="o in list_number_of_electors" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','number_of_electors')">{{form['number_of_electors']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','course_selection_content') || $check_field('add','course_selection_content') || $check_field('set','course_selection_content')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="选课内容" prop="course_selection_content">
								<el-input type="textarea" id="course_selection_content" v-model="form['course_selection_content']" placeholder="请输入选课内容"
						v-if="(form['course_selection_record_id'] && $check_field('set','course_selection_content')) || (!form['course_selection_record_id'] && $check_field('add','course_selection_content'))" :disabled="disabledObj['course_selection_content_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','course_selection_content')">{{form['course_selection_content']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','course_selection_notes') || $check_field('add','course_selection_notes') || $check_field('set','course_selection_notes')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="选课备注" prop="course_selection_notes">
								<el-input type="textarea" id="course_selection_notes" v-model="form['course_selection_notes']" placeholder="请输入选课备注"
						v-if="(form['course_selection_record_id'] && $check_field('set','course_selection_notes')) || (!form['course_selection_record_id'] && $check_field('add','course_selection_notes'))" :disabled="disabledObj['course_selection_notes_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','course_selection_notes')">{{form['course_selection_notes']}}</div>
							</el-form-item>
			</el-col>
						
	
	
		
		
						<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp" v-if="!form['source_user_id'] && 0 > 0">
			  <el-form-item label="取消选课限次">
				<el-input id="limit_times" v-model="form['cancel_course_selection_limit_times']" placeholder="取消选课限制次数，0为不限"
						  v-if="$check_option('/course_selection_record/view','can_limits')"
				></el-input>
				<div v-else-if="$check_action('/course_selection_record/view','get')" v-html="form['cancel_course_selection_limit_times']"></div>
			  </el-form-item>
			</el-col>
			
	
	
	
	</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/course_selection_record/view','set') || $check_action('/course_selection_record/view','add') || $check_option('/course_selection_record/table','examine')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "course_selection_record_id",
				url_add: "~/api/course_selection_record/add?",
				url_set: "~/api/course_selection_record/set?",
				url_get_obj: "~/api/course_selection_record/get_obj?",
				url_upload: "~/api/course_selection_record/upload?",

				query: {
					"course_selection_record_id": 0,
				},

				form: {
								"club_name":  '', // 社团名称
										"course_name":  '', // 课程名称
										"maximum_number_of_people":  0, // 人数上限
										"current_number_of_people":  0, // 当前人数
										"teacher_users": 0, // 教师用户
										"student_users": 0, // 学生用户
										"student_name":  '', // 学生姓名
										"student_phone_number":  '', // 学生电话
										"student_id":  '', // 学生学号
										"number_of_electors":  '', // 选课人数
										"course_selection_content":  '', // 选课内容
										"course_selection_notes":  '', // 选课备注
											"course_selection_record_id": 0, // ID
													"cancel_course_selection_limit_times": 0, // 取消选课限制次数
										},
				disabledObj:{
								"club_name_isDisabled": false,
										"course_name_isDisabled": false,
					          			"maximum_number_of_people_isDisabled": false,
					          			"current_number_of_people_isDisabled": false,
										"teacher_users_isDisabled": false,
										"student_users_isDisabled": false,
										"student_name_isDisabled": false,
										"student_phone_number_isDisabled": false,
										"student_id_isDisabled": false,
										"number_of_electors_isDisabled": false,
										"course_selection_content_isDisabled": false,
										"course_selection_notes_isDisabled": false,
										},

	
				
				
				
				
					// 用户列表
				list_user_teacher_users: [],
						
					// 用户列表
				list_user_student_users: [],
						// 用户组
				group_user_student_users: "",
						
				
				
										// 选课人数选项列表
				list_number_of_electors: ['1'],
	
				
				
			
			}
		},
		methods: {

	
	
			
	
			
	
			
	
			
	
				/**
			 * 获取教师用户用户列表
			 */
			async get_list_user_teacher_users() {
                var json = await this.$get("~/api/user/get_list?user_group=教师用户");
                if(json.result && json.result.list){
                    this.list_user_teacher_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					get_user_teacher_users(id){
				var obj = this.list_user_teacher_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
				/**
			 * 获取学生用户用户列表
			 */
			async get_list_user_student_users() {
                var json = await this.$get("~/api/user/get_list?user_group=学生用户");
                if(json.result && json.result.list){
                    this.list_user_student_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取学生用户用户组
			 */
			async get_group_user_student_users() {
							this.form["student_users"] = this.$store.state.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=学生用户");
				if(json.result && json.result.obj){
					this.group_user_student_users = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_student_users(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_student_users.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["student_users"] = id
									_this.disabledObj['student_users' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "student_users") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_student_users(id){
				var obj = this.list_user_student_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
			
	
			
	
			
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
																			// 获取缓存数据附加
				form = $.db.get("form");
									$.push(this.form ,form);
																												if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
																												$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){

																																																												

			},

																																																																																										async submit(param, func){
				if (!param) {
					param = this.form;
				}
								var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																																																												ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
																																																																																																																																														return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/course_selection_record/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/course_selection_record/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/course_selection_record/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/course_selection_record/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/course_selection_record/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
													this.get_list_user_teacher_users();
								this.get_list_user_student_users();
					this.get_group_user_student_users();
																	},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




</style>
